<?php

namespace App\Models\Form;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class RootForm
{
    private $errors;
    private array $data;
    protected array $dbData;

    public function __construct(array $attributes = [])
    {
        foreach ($attributes as $k => $v) {
            if (property_exists($this, $k)) $this->$k = $v;
            $this->data[$k] = $v;
        }
    }

    public function validate($data, $rules, $msg, $attr)
    {
        $v = Validator::make($data, $rules, $msg, $attr);
        if ($v->fails())
            $this->setErrors($v->errors()->getMessages());
    }

    public function isValid()
    {
        return empty($this->errors);
    }

    public function checkVCode($vcode)
    {
        $origin = "123456";
        return $vcode == $origin;
    }

    /**
     * @return mixed
     */
    public function getErrors()
    {
        return $this->errors;
    }

    /**
     * @param mixed $errors
     */
    private function setErrors($errors): void
    {
        $this->errors = $errors;
    }

    public function getData()
    {
        return $this->data;
    }

    public function getDBData()
    {
        return $this->dbData;
    }
}
